﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FreeTextBoxControls;
using System.Drawing;
namespace TCS.Web.Admin.MemberManage
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Session["strWhereMemberManage"] = "";
                BindSuppData();
                SetRights();
                grid.BorderColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_bordercolorlight"].ToString());
                grid.HeaderStyle.BackColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString());

                Confirm.Attributes.Add("onclick", "return confirm('删除该用户将随同删除相应的显示记录和网址关联信息！\\n你是否确定删除这些记录？');");
                int pageIndex = 1;
                if (Request.Params["page"] != null && Request.Params["page"].ToString() != "")
                {
                    Session["pageNewsr"] = Convert.ToInt32(Request.Params["page"]);
                    pageIndex = Convert.ToInt32(Request.Params["page"]);
                }
                else
                {
                    if (Session["pageNewsr"] != null && Session["pageNewsr"].ToString() != "")
                    {
                        pageIndex = Convert.ToInt32(Session["pageNewsr"]);
                    }
                    else
                    {
                        pageIndex = 1;
                        Session["pageNewsr"] = 1;
                    }
                }

                dataBind(pageIndex);
            }
        }
        private void BindSuppData()
        {
            TCS.BLL.Gsp_Administrative bll = new TCS.BLL.Gsp_Administrative();
            this.dropArea.DataSource = bll.GetAllList();
            this.dropArea.DataTextField = "addName";
            this.dropArea.DataValueField = "addId";
            this.dropArea.DataBind();

            this.dropArea.Items.Insert(0, new ListItem("-请选择所属区域-", "-1"));
        }
        private void SetRights()
        {
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            string dpid = new LTP.Accounts.Bus.User().GetUserdepartmentID(Context.User.Identity.Name);
            if (dpid != "1")
            {
                dropArea.SelectedIndex = -1;
                dropArea.Items.FindByValue(dpid).Selected = true;
                dropArea.Enabled = false;
            }
        }
        private void dataBind(int pageIndex)
        {
        //    this.Page011.Page_Search = "";
        //    this.Page011.Page_Makesql = "";
           
        //    string strWhere = "";
        //    string dpid = new LTP.Accounts.Bus.User().GetUserdepartmentID(Context.User.Identity.Name);
        //    if (dpid != "1")
        //    {
        //        strWhere = " (1=1) and AddID=" + dpid + "";
        //    }
        //    else
        //    {
        //        strWhere = " (1=1)";
        //    }
        //    pageIndex--;
        //    TCS.BLL.MemberInfo bll = new TCS.BLL.MemberInfo();
            
        //    string strorder = "";
        //    if (Session["strWhereMemberManage"] != null && Session["strWhereMemberManage"].ToString() != "")
        //    {
        //        strWhere += Session["strWhereMemberManage"].ToString();
        //    }
        //    if (Session["strorderby"] != null && Session["strorderby"].ToString() != "")
        //    {
        //        strorder= Session["strorderby"].ToString();
        //    }
        //    DataSet ds = new DataSet();
        //    ds = bll.GetList(strWhere, strorder);
        //    grid.DataSource = ds.Tables[0].DefaultView;
        //    int record_Count = ds.Tables[0].Rows.Count;
        //    int page_Size = grid.PageSize;
        //    int totalPages = int.Parse(Math.Ceiling((double)record_Count / page_Size).ToString());
        //    if (totalPages > 0)
        //    {
        //        if ((pageIndex + 1) > totalPages)
        //            pageIndex = totalPages - 1;
        //    }
        //    else
        //    {
        //        pageIndex = 0;
        //    }
        //    grid.CurrentPageIndex = pageIndex;
        //    grid.DataBind();
        //    int page_Count = grid.PageCount;
        //    int page_Current = pageIndex + 1;

        //    Page011.Record_Count = record_Count;
        //    Page011.Page_Count = page_Count;
        //    Page021.Page_Count = page_Count;

        //    Page011.Page_Size = page_Size;
        //    Page021.Page_Size = page_Size;
        //    Page011.Page_Current = page_Current;
        //    Page021.Page_Current = page_Current;
        //}
        //protected void btn_Search_Click(object sender, ImageClickEventArgs e)
        //{
        //    string strsql = "";
        //    string adname = this.txtKey.Text.Trim();
        //    string field = this.DropField.SelectedValue;
        //    string addid = this.dropArea.SelectedValue;
        //    if (adname != "" && adname != "请输入关键字")
        //    {
        //        strsql += " and (" + field + " like'%" + adname + "%')";
        //    }
        //    if (addid != "-1")
        //    {
        //        strsql += " and addid="+addid;
        //    }
        //    if (strsql != "")
        //    {
        //        Session["strWhereMemberManage"] = " " + strsql;
        //    }
        //    else
        //    {
        //        Session["strWhereMemberManage"] = "";
        //    }
        //    dataBind(1);
        //    //Response.Redirect("index.aspx?page=1");
        //}
        //protected void btn_Relese_Click(object sender, EventArgs e)
        //{
        //    TCS.BLL.MemberInfo bll = new TCS.BLL.MemberInfo();
        //    string idlist = GetSelIDlist();
        //    if (idlist == "")
        //    {
        //        LTP.Common.MessageBox.Show(this, "没有选择项！");
        //        return;
        //    }
        //    bll.NoCancelList(idlist);
        //    Response.Redirect("index.aspx");

        //}
        //protected void btn_NoRelease_Click(object sender, EventArgs e)
        //{
        //    TCS.BLL.MemberInfo bll = new TCS.BLL.MemberInfo();
        //    string idlist = GetSelIDlist();
        //    if (idlist == "")
        //    {
        //        LTP.Common.MessageBox.Show(this, "没有选择项！");
        //        return;
        //    }
        //    bll.CancelList(idlist);
        //    Response.Redirect("index.aspx");

        }
        protected void Confirm_Click(object sender, EventArgs e)
        {
            //TCS.BLL.NewsManage.News bll = new TCS.BLL.NewsManage.News();
            //string idlist = GetSelIDlist();
            //if (idlist == "")
            //{
            //    LTP.Common.MessageBox.Show(this, "没有选择项！");
            //    return;
            //}
            //bll.DeleteList(idlist);
            //Response.Redirect("index.aspx");
        }
        private string GetSelIDlist()
        {
            string dgIDs = "";
            bool BxsChkd = false;
            foreach (DataGridItem item in grid.Items)
            {
                CheckBox deleteChkBxItem = (CheckBox)item.FindControl("SelectThis");
                if (deleteChkBxItem.Checked)
                {
                    BxsChkd = true;
                    dgIDs += item.Cells[1].Text + ",";
                }
            }
            if (BxsChkd)
            {
                dgIDs = dgIDs.Substring(0, dgIDs.LastIndexOf(","));
            }
            return dgIDs;
        }
        protected void grid_ItemDataBound(object sender, DataGridItemEventArgs e)
        {

            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                string Dormancy =DataBinder.Eval(e.Item.DataItem, "IsDelete").ToString();
                int Addid = (int)DataBinder.Eval(e.Item.DataItem, "AddID");
                int MemberID = (int)DataBinder.Eval(e.Item.DataItem, "MemberID");
                if (Dormancy.ToLower() == "1")
                {
                    e.Item.Cells[5].Text = "<span style=\" color:Red\">已注销</span>";
                }
                else
                {
                    e.Item.Cells[5].Text = "<span style=\" color:Green\">正常</span>";
                }

                TCS.BLL.Gsp_Administrative bll = new TCS.BLL.Gsp_Administrative();
                TCS.Model.Gsp_Administrative mod =  bll.GetModelByCache(Addid);
                if (mod != null)
                {
                    e.Item.Cells[6].Text = mod.AddName;
                }
                else
                {
                    e.Item.Cells[6].Text = "未知";
                }
            }
        }

        protected void grid_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
          
            Session["strorderby"] =  e.SortExpression;
            int pageIndex = 1;
            if (Request.Params["page"] != null && Request.Params["page"].ToString() != "")
            {
                Session["pageNewsr"] = Convert.ToInt32(Request.Params["page"]);
                pageIndex = Convert.ToInt32(Request.Params["page"]);
            }
            else
            {
                if (Session["pageNewsr"] != null && Session["pageNewsr"].ToString() != "")
                {
                    pageIndex = Convert.ToInt32(Session["pageNewsr"]);
                }
                else
                {
                    pageIndex = 1;
                    Session["pageNewsr"] = 1;
                }
            }
            dataBind(pageIndex);
        }
        protected string GetNum(string n)
        {
            if ((this.grid.CurrentPageIndex + 1) > 1)
            {
                return Convert.ToString((grid.CurrentPageIndex) * grid.PageSize + (int.Parse(n) + 1));
            }
            else
            {
                return Convert.ToString(int.Parse(n) + 1);
            }
        }
    }
}
